package com.mooc.fileServlet;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;

import org.junit.Test;
/**
 * @Author: 范海华
 * @Description: 描述
 * @Date: 2021/11/2 17:17
 */
public class HttpClient1 {
	@Test
    public void up() throws IOException {
        DataInputStream input=null;
        OutputStream out=null;
        HttpURLConnection conn=null;
        JSONObject repost=null;
        InputStream in=null;
        ByteArrayOutputStream outputStream=null;
        try {
            URL url=new URL("http://localhost:8080/QiyuesuoTest1/test/fileupLoad?methodName=up&fileName=aa.txt");
            conn=(HttpURLConnection) url.openConnection();
            //发送post请求
            conn.setDoOutput(true);
            conn.setUseCaches(false);
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Content-Type","text/html");
            conn.setRequestProperty("Cache-Control","no-cache");
            conn.setRequestProperty("Charset","UTF-8");
            conn.connect();;
            conn.setConnectTimeout(10000);
            out =conn.getOutputStream();

            File file = new File("E:/AA.txt");
            in= new DataInputStream(new FileInputStream(file));

            int bytes=0;
            byte[] buffer=new byte[1024];
            while((bytes=in.read(buffer))!=-1){
                out.write(buffer,0,bytes);
            }
            out.flush();
            if(conn.getResponseCode()==HttpURLConnection.HTTP_OK){
                in= conn.getInputStream();
                outputStream =new ByteArrayOutputStream();
                byte[] data =new byte[1024];
                int count=-1;
                while((count=in.read(data))!=-1){
                    outputStream.write(data,0,count);
                }
                data=null;
                repost = JSON.parseObject(new String(outputStream.toByteArray(),"UTF-8"));
                System.out.println("-------------------");
                System.out.println(repost);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if (in != null) {
                in.close();
            }
            if (out != null) {
                out.close();
            }
            if (in != null) {
                in.close();
            }
            if (out!= null) {
                out.close();
            }
            if (conn != null) {
                conn.disconnect();
            }
        }
    }
	 @Test
	 public void down() throws IOException {
		 ByteArrayOutputStream outputStream=null;
		 HttpURLConnection conn=null;
		 InputStream in=null;
		 OutputStream out=null;
		 URL url=new URL("http://localhost:8080/QiyuesuoTest1/test/fileupLoad?methodName=down&uuid=?");
         conn=(HttpURLConnection) url.openConnection();
         //发送post请求
         conn.setDoOutput(true);
         conn.setUseCaches(false);
         conn.setRequestMethod("POST");
         conn.setRequestProperty("Content-Type","text/html");
         conn.setRequestProperty("Cache-Control","no-cache");
         conn.setRequestProperty("Charset","UTF-8");
         conn.connect();;
         conn.setConnectTimeout(10000);
         out =conn.getOutputStream();
         
         
         File file = new File("E:/CC.txt");
         out =new FileOutputStream(file);
         int bytes=0;
         byte[] buffer=new byte[1024];
         while((bytes=in.read(buffer))!=-1){
             out.write(buffer,0,bytes);
         }

         out.flush();
		 out.close();  
	 }
	 
	 @Test
	 public void getYuan() throws IOException {
		 ByteArrayOutputStream outputStream=null;
		 HttpURLConnection conn=null;
		 InputStream in=null;
		 JSONObject repost=null;
		 URL url=new URL("http://localhost:8080/QiyuesuoTest1/test/fileupLoad?methodName=yuan&uuid=aa.txt");
         conn=(HttpURLConnection) url.openConnection();
         //发送post请求
         conn.setDoOutput(true);
         conn.setUseCaches(false);
         conn.setRequestMethod("GET");
         conn.setRequestProperty("Content-Type","text/html");
         conn.setRequestProperty("Cache-Control","no-cache");
         conn.setRequestProperty("Charset","UTF-8");
         conn.connect();;
         conn.setConnectTimeout(10000);

         if(conn.getResponseCode()==HttpURLConnection.HTTP_OK){
             in= conn.getInputStream();
             outputStream =new ByteArrayOutputStream();
             byte[] data =new byte[1024];
             int count=-1;
             while((count=in.read(data))!=-1){
            	 System.out.println("读取到的长度是"+count);
                 outputStream.write(data,0,count);
             }
             data=null;
             repost = JSON.parseObject(new String(outputStream.toByteArray(),"UTF-8"));
             System.out.println("------------------------");
             System.out.println(repost);
         }
	 }
}
